﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using HouodeIT.CloudePlatform.Common.Enum;
using HouodeIT.CloudePlatform.Common.Sql;
using HouodeIT.CloudePlatform.IPL;
using HouodeIT.CloudePlatform.Model;
using HouodeIT.CloudePlatform.PL;
using HouodeIT.CloudePlatform.PL.Factory;
using PetaPoco;

namespace HouodeIT.CloudePlatform.ConsoleTest
{
    class Program
    {
        private static void Main(string[] args)
        {
            #region Test: Thread

            //Thread t1 = new Thread(test);
            //Thread t2 = new Thread(test);
            //Thread t3 = new Thread(test);
            //Thread t4 = new Thread(test);
            //Thread t5 = new Thread(test);
            //Thread t6 = new Thread(test);
            //Thread t7 = new Thread(test);
            //Thread t8 = new Thread(test);
            //t1.Start("t1");
            //t2.Start("t2");
            //t3.Start("t3");
            //t4.Start("t4");
            //t5.Start("t5");
            //t6.Start("t6");
            //t7.Start("t7");
            //t8.Start("t8"); 

            #endregion

            #region Test: Dict1

            //Dictionary<string,string> dict=new Dictionary<string, string>();
            //dict.Add("ad","adf");
            //dict.Add("asd","adf");
            //dict.Add("ee","adf");
            //dict.Add("11","adf");
            //dict.Add("gr","adf");
            //dict.Add("34","adf");
            //dict.Add("cc","adf");
            //dict.Add("yt","adf");
            //dict.Add("iu","adf");
            //dict.Add("cc","adf");
            //dict.Add("aq","adf"); 

            #endregion

            #region Test: Dict2

            //Dictionary<object,string> dictionary=new Dictionary<object, string>();

            //UserInfo u=new UserInfo();
            //u.UserName = "123";
            //u.UserAddress = "123";
            //dictionary.Add(u,"1");
            //dictionary.Add(u,"2");
            //dictionary.Add(u,"3");
            //dictionary.Add(u,"4");


            #endregion

            #region Test: lambda

            ////register a lambda tree
            //Expression<Func<UserInfo, bool>> lambdaParam1 = u => u.UserId < 0 && u.UserName == "admin" && u.UserAddress == "123";
            //Expression<Func<UserInfo>> lambdaParam2 = () => new UserInfo { UserId = 0, UserName = "admin", UserAddress = "123" };
            ////get lambda body
            //var body1 = lambdaParam1.Body;
            ////replace body string to sql where
            //var body1Str = body1.ToString().Replace("(", "").Replace(")", "").Replace("AndAlso", "and");
            //body1Str = Regex.Replace(body1Str, "(<=*)|(>=*)", "<=");
            ////get set body collection
            //var body2 = lambdaParam2.Body as MemberInitExpression;
            //var bodyCollection = body2.Bindings;

            ////get lambda param, it is a collection
            //var param1 = lambdaParam1.Parameters;
            //var param2 = lambdaParam2.Parameters;
            ////get type of lambda
            //var type1 = lambdaParam1.Type;
            //var type2 = lambdaParam2.Type;
            ////get lambda return type
            //var returnType1 = lambdaParam1.ReturnType;
            //var returnType2 = lambdaParam2.ReturnType;
            ////get lambda note type
            //var noteType1 = lambdaParam1.NodeType;
            //var noteType2 = lambdaParam2.NodeType;

            #endregion

            #region Test: Get T name

            //var x = GetTName<UserInfo>();

            #endregion

            #region Test: PLSession

            //var p = new PLSession();
            //var uInsert = new UserInfo() { UserAddress = "123", UserName = "111" };
            //var actioninfo = new ActionInfo { ActionName = "超级管理员" };
            //for (int i = 0; i < 10; i++)
            //{
            //    p.Insert(uInsert);
            //    p.Insert(actioninfo);
            //}
            //for (int i = 0; i < 5; i++)
            //{
            //    p.RegistEntity(uInsert, EntityState.Add);
            //    p.Insert(actioninfo);
            //}
            //p.Edit(new UserInfo() { UserAddress = "mmm", UserName = "mmm", UserId = 1 });
            //p.Edit(new UserInfo() { UserAddress = "nnn", UserName = "nnn", UserId = 2 });
            //p.Edit(new UserInfo() { UserAddress = "zzz", UserName = "zzz", UserId = 3 });
            //p.RegistEntity(new UserInfo() { UserAddress = "register", UserName = "register", UserId = 4 }, EntityState.Edit);

            //const string editSql = "update userinfo set username = @a1 where userid = @a2";

            //p.Edit(editSql, new { a1 = "aaaaa", a2 = 5 });
            //p.Edit(new UserInfo { UserAddress = "register", UserName = "register", UserId = 10 });

            //p.Edit(() => new UserInfo { UserName = "lambda", UserAddress = "lambda" }, uu => uu.UserId == 12);
            //p.Edit(() => new UserInfo { ActionId = 1 }, null);
            //p.Edit(() => new ActionInfo { ActionName = "极品。。。" }, null);
            //p.Commit();

            #endregion

            #region Test: dynamic to object[]

            //dynamic d1 = new { x = 1, y = 2, z = 3 };
            //dynamic d2 = new[] { new { x = 1, y = 2, z = 3 }, new { x = 4, y = 5, z = 6 } };

            //var m1 = d1 as object;
            //var n1 = d1 as object[];
            //var m2 = d2 as object;
            //var n2 = d2 as object[];

            #endregion

            #region Test: SqlCount
            //string sql = string.Format("select UserId, UserName, UserAddress from (select row_number() over(order by UserId desc) as rk,  UserId, UserName, UserAddress  from userinfo) u where rk between {0} and {1} ", 1, 3);
            //Database _db = DbContextFactory.GetCurrentContext("sqlserver");
            //string sqlCount, sqlPage;
            //var arg = new object[] { new { x = 1, y = 2 } };
            //_db.BuildPageQueries<UserInfo>(1 * 1 - 1, 1, "", ref arg, out sqlCount, out sqlPage);

            #endregion

            #region Test: SqlBuilder
            //var p = new PLSession();
            //var sql = Sql.Builder.Select("u.userId", "u.userName", "u.actionId")
            //        .From("Userinfo u")
            //        .LeftJoin("actioninfo a")
            //        .On("u.ActionId = a.ActionId")
            //        .Where("userName=@a111", new { a111 = "111" });
            //Database db = new Database("sqlserver");
            //var u1 = db.Query<UserInfo>(sql).ToList();
            ////Database db = new Database("sqlserver");
            //var u2 = db.Query<UserInfo>(sql.SQL, new object[] { "111" });

            //long total = 0;
            //var u3 = p.QueryPage<UserInfo>(1, 10, out total, sql.SQL, new object[] { "111" }).ToList();
            #endregion

            Console.ReadKey();
        }

        /// <summary>
        /// test to insert to database
        /// </summary>
        /// <param name="c"></param>
        public static void Test(object c)
        {
            using (Database db = new Database("sqlserver"))
            {
                for (int i = 0; i < 100; i++)
                {
                    string sql = string.Format("insert into userinfo values( 'admin{0}', '123')", c);
                    db.Execute(sql);
                }

            }

        }

        public static dynamic GetTName<T>()
        {
            return typeof(T).Name;
        }

    }
}
